home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Snippets / Devices / ModifyMouseAccl / ModifyMouseAccl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-25  |  6.6 KB  |  249 lines  |  [TEXT/MMCC]

  1. /*
  2.     File: ModifyMouseAccl.c
  3.     By Rich Kubota
  4.     Developer Technical Support
  5.     
  6.     Snippet to demonstrate how one uses the CursorDevice Manager to change the 
  7.     acceleration curve setting associated with the standard Apple mouse.
  8.     
  9.     You can select an extremely sensitive or a tablet-like acceleration setting
  10.     by setting the NOACCLCURVE define appropriately.
  11. */
  12.  
  13. #include <Types.h>
  14. #include <CursorDevices.h>
  15. #include "CrsrDev.h"
  16. #include <stdio.h>
  17. #include <LowMem.h>
  18.  
  19. #define NOACCLCURVE        0    // set to 0 for a really zippy mouse
  20.                             // set to 1 for a tablet style mouse - one with no acceleration
  21.  
  22. #define mapTrue            0xFFFF
  23. #define acclRsrcType    'accl'
  24. #define k200dpiMouseID    '@200'
  25.  
  26.     // the following points are from the ROM sources for the "@200"
  27.     // 200 dpi mouse accl setting for the upper curve
  28.     // leave the original values alone so that we can restore the 
  29.     // setting
  30.     // here are the original values    
  31. AcclPoint    gOrigAcclPt[9] =    {{0x0000713B, 0x00006000},
  32.                                  {0x00044EC5, 0x00108000},
  33.                                  {0x000C0000, 0x005F0000},
  34.                                  {0x0016EC4F, 0x008B0000},
  35.                                  {0x001D3B14, 0x00948000},
  36.                                  {0x00227627, 0x00960000},
  37.                                  {0x00246276, 0x00960000},
  38.                                  {0x00260000, 0x00960000},
  39.                                  {0x00280000, 0x00960000}};
  40.  
  41.  
  42.     // here are the replacement values for test
  43.     // modify these on a trial and error basis.
  44. #if NOACCLCURVE
  45. AcclPoint    gReplaceAcclPt[9] = {{0x00010000, 0x00010000},
  46.                                  {0x00010000, 0x00010000},
  47.                                  {0x00010000, 0x00010000},
  48.                                  {0x00010000, 0x00010000},
  49.                                  {0x00010000, 0x00010000},
  50.                                  {0x00010000, 0x00010000},
  51.                                  {0x00010000, 0x00010000},
  52.                                  {0x00010000, 0x00010000},
  53.                                  {0x00010000, 0x00010000}};
  54. #else
  55.  
  56. AcclPoint    gReplaceAcclPt[9] = {{0x00000400, 0x00006000},
  57.                                  {0x00000800, 0x00108000},
  58.                                  {0x00000C00, 0x00600000},
  59.                                  {0x00001000, 0x00A00000},
  60.                                  {0x00002000, 0x00C08000},
  61.                                  {0x00004000, 0x01000000},
  62.                                  {0x00008000, 0x02000000},
  63.                                  {0x0000C000, 0x04000000},
  64.                                  {0x00010000, 0x08000000}};
  65.  
  66. #endif
  67.  
  68. CursorDevicePtr    GetMouseCursorDeviceRec(UInt32 devID);
  69. AcclRsrcHandle    GetAcclRsrc(UInt32 devID);
  70. OSErr ModifySecond200dpiTable(AcclRsrcHandle acclRsrcHndl, 
  71.                         AcclPoint *acclPtr, short numPts);
  72.  
  73. main()
  74. {
  75.     CursorDevicePtr    cdPtr;
  76.     AcclRsrcHandle    acclRsrcHndl, saveHndl;
  77.     Fixed            accel = 0x00010000;
  78.     Size            hsize;
  79.     OSErr            err;
  80.     
  81.     cdPtr = GetMouseCursorDeviceRec((UInt32)k200dpiMouseID);
  82.     if (cdPtr == NULL)
  83.     {        // device not found
  84.         printf("\nDevice record for 200 dpi mouse not found");
  85.         return;
  86.     }
  87.     
  88.     acclRsrcHndl = GetAcclRsrc((UInt32)k200dpiMouseID);
  89.     if (acclRsrcHndl == NULL)
  90.     {        // device not found
  91.         printf("\naccl resource for 200 dpi mouse not found");
  92.         return;
  93.     }
  94.     
  95.         // save a copy of the handle
  96.     hsize = GetHandleSize((Handle)acclRsrcHndl);
  97.     if (hsize < 0)
  98.     {
  99.         printf("\nerror getting handle size");
  100.         return;
  101.     }
  102.  
  103.     saveHndl = (AcclRsrcHandle)NewHandle(hsize);
  104.     
  105.     if (saveHndl == nil)
  106.     {
  107.         printf("\nerror allocating the save handle");
  108.         return;
  109.     }
  110.  
  111.         // copy contents of the handle
  112.     BlockMove((Ptr)*acclRsrcHndl, (Ptr)*saveHndl, hsize);
  113.     
  114.         // check whether there are two tables for the 200 dpi accl resource
  115.     if ((*acclRsrcHndl)->numAcclTables <= 1)
  116.     {        // device not recognized
  117.         printf("\naccl resource for 200 dpi mouse not recognized");
  118.         return;
  119.     }
  120.  
  121.         // modify 200 dpi table using modified values
  122.     err = ModifySecond200dpiTable(acclRsrcHndl, (AcclPoint*)gReplaceAcclPt,
  123.                                 sizeof(gReplaceAcclPt)/sizeof(AcclPoint));
  124.     if (err != noErr)
  125.     {        // error adding modified values
  126.         printf("\nerror adding modified values - %d", err);
  127.         return;
  128.     }
  129.     
  130.         // the resource has been modified but we need to set the
  131.         // acceleration to have the modified accl values take effect
  132.     CursorDeviceSetAcceleration(cdPtr, accel);
  133.     
  134.     printf("\nthe modified accl field has been installed");
  135.     printf("\nclick the mouse when finished");
  136.     printf("\n");
  137.     printf("\n");
  138.     
  139.     while (!Button());
  140.  
  141.         // restore original accl values
  142.         
  143.         // copy contents of the handle
  144.     BlockMove((Ptr)*saveHndl, (Ptr)*acclRsrcHndl, hsize);
  145.  
  146.         // the resource has been modified but we need to set the
  147.         // acceleration to have the modified accl values take effect
  148.     CursorDeviceSetAcceleration(cdPtr, accel);
  149.     
  150. }
  151.  
  152. OSErr ModifySecond200dpiTable(AcclRsrcHandle acclRsrcHndl, AcclPoint *acclPtr,
  153.                                 short numPts)
  154. {
  155.     AcclTablePtr    acclTablePtr;
  156.     ResType            rType;
  157.     short            sizeTable1, sizeTable2;
  158.     short            resSave, sysRefNum;
  159.     short            rID;
  160.     OSErr            err;
  161.     Str255            rName;
  162.  
  163.         // get the resource number for the accl resource
  164.     GetResInfo((Handle)acclRsrcHndl, &rID, &rType, rName);
  165.     if (err = ResError())
  166.         return err;
  167.         
  168.         // get the size of the first table
  169.     sizeTable1 = (*acclRsrcHndl)->acclTable[0].numAcclPoints;
  170.     sizeTable1--;    // subtract off the size of the first entry
  171.     
  172.         // set the acclTablePtr to point to the second table
  173.     acclTablePtr = (AcclTablePtr)((long)&(*acclRsrcHndl)->acclTable[1] + 
  174.                             sizeTable1 * sizeof(AcclPoint));
  175.                             
  176.         // get the number of accl points in the second table
  177.     sizeTable2 = acclTablePtr->numAcclPoints;
  178.         
  179.         // set sizeTable2 to the lesser of itself and numPts
  180.     sizeTable2 = sizeTable2 > numPts ? numPts : sizeTable2;
  181.     
  182.     BlockMove((Ptr)acclPtr, (Ptr)&acclTablePtr->acclPoints,
  183.                     sizeTable2 * sizeof(AcclPoint));
  184.     
  185.     return err;            
  186. /*
  187.     GetMouseCursorDeviceRec finds a CursorDevice record with a device ID matching
  188.     the devID input parameter
  189. */
  190. CursorDevicePtr    GetMouseCursorDeviceRec(UInt32 devID)
  191. {
  192.     CursorDevicePtr cdPtr;
  193.     Boolean            done = false;
  194.  
  195.     cdPtr = NULL;
  196.     while (done == false)
  197.     {
  198.         CursorDeviceNextDevice(&cdPtr);
  199.         if (cdPtr->devID == (OSType)devID)    // is this the record that we want
  200.             done = true;
  201.         else if (cdPtr->nextCursorDevice == NULL)    // is this the last record in the chain
  202.         {
  203.             done = true;
  204.             cdPtr = NULL;
  205.         }
  206. //        else    look at the next record
  207.     }
  208.     return cdPtr;
  209.  
  210. }
  211.  
  212. AcclRsrcHandle    GetAcclRsrc(UInt32 devID)
  213. {
  214.     AcclRsrcHandle    acclHndl;
  215.     short            numAcclRsrc;
  216.     short            i;
  217.     Boolean            done = false;
  218.     UInt8            romMapSave;
  219.     
  220.     romMapSave = LMGetROMMapInsert();    // get the state of the ROM Map Insert;
  221.     LMSetROMMapInsert(mapTrue);            // map the ROM into the resource chain
  222.  
  223.     numAcclRsrc = CountResources(acclRsrcType);
  224.     
  225.     i = 0;        // initialize our index
  226.     
  227.     while (done == false)
  228.     {
  229.         if (i >= numAcclRsrc)    // is the index past the number of 'accl' resources
  230.         {
  231.             done = true;        // let's bail
  232.             acclHndl = NULL;    // set result
  233.         }
  234.         else
  235.         {
  236.             i++;    // increment the counter since we start at 0
  237.             acclHndl = (AcclRsrcHandle)GetIndResource(acclRsrcType, i);
  238.             
  239.             if ((*acclHndl)->acclClass == (UInt32)devID)
  240.             {
  241.                 done = true;
  242.             }
  243.         }
  244.     }
  245.     
  246.     LMSetROMMapInsert(romMapSave);            // restore ROM Map insert state
  247.     return acclHndl;
  248. }